package com.mhl.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.mhl.campuscabinet.R;
import com.mhl.entity.SoldProduct;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class SoldProductAdapter extends RecyclerView.Adapter<SoldProductAdapter.ViewHolder> {
    // 所需要展示的订单的实体List
    private List<SoldProduct> mOrderList;
    private AppCompatActivity activity;

    // 内部静态类负责对已加载的商品布局信息进行缓存
    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView productImage;
        TextView productName;
        TextView solder;
        TextView buyer;
        TextView time;
        TextView price;

        public ViewHolder(View view) {
            super(view);
            productImage = view.findViewById(R.id.product_image);
            productName = view.findViewById(R.id.product_name);
            buyer = view.findViewById(R.id.buyer_name);
            solder = view.findViewById(R.id.solder_name);
            time = view.findViewById(R.id.time);
            price = view.findViewById(R.id.price);
        }
    }

    // 构造方法
    public SoldProductAdapter(List<SoldProduct> orderList, AppCompatActivity activity){
        this.activity = activity;
        mOrderList = orderList;
    }
    @NonNull
    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.order_item, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull SoldProductAdapter.ViewHolder holder, int position) {
        SoldProduct soldProduct = mOrderList.get(position);
        // 加载商品图片
        System.out.println("商品的url:"+soldProduct.getUrls());

        Glide.with(activity).load(soldProduct.getUrls().get(0)).into(holder.productImage);
        holder.productName.setText(soldProduct.getName());
        holder.solder.setText(soldProduct.getSolder());
        holder.buyer.setText(soldProduct.getBuyer());
        holder.time.setText(soldProduct.getTime());
        holder.price.setText(new StringBuilder().append("￥").append(soldProduct.getPrice()).toString());
    }

    @Override
    public int getItemCount() {
        return mOrderList.size();
    }



}
